//定义商品分类全局组件的小仓库
import { defineStore } from 'pinia'
//引入接口返回数据类型
import type { CategoryResponseData } from '@/apis/product/attr/type'
import type { CategoryState } from './types/types'
//引入分类接口方法
import { reqC1, reqC2, reqC3 } from '@/apis/product/attr/index'

let useCategoryStore = defineStore('Category', {
  state: (): CategoryState => {
    return {
      //存储一级分类的数据
      c1Arr: [],
      //存储一级分类的ID
      c1Id: '',
      //存储对应二级分类的数据
      c2Arr: [],
      //收集二级分类的id
      c2Id: '',
      //存储对应三级分类的数据
      c3Arr: [],
      //收集三级分类的id
      c3Id: '',
    }
  },
  actions: {
    //获取一级分类的方法
    async getC1() {
      //发请求获取一级分类的数据
      let result: CategoryResponseData = await reqC1()
      if (result.code === 200) {
        this.c1Arr = result.data
      }
    },
    async getC2() {
      //发请求获取一级分类下的二级分类的数据
      let result: CategoryResponseData = await reqC2(this.c1Id)
      if (result.code === 200) {
        this.c2Arr = result.data
      }
    },
    async getC3() {
      //发请求获取一级分类下的二级分类的数据
      let result: CategoryResponseData = await reqC3(this.c2Id)
      if (result.code === 200) {
        this.c3Arr = result.data
      }
    },
  },
})
// let useCategoryStore = defineStore('Category', () => {
//     let c1Arr: any = ref([])
//     async function getC1() {
//         //发请求获取一级分类的数据
//         let result: any = await reqC1()
//         if (result.code === 200) {
//             c1Arr.value = result.data
//             console.log(c1Arr.value);

//         }
//     }
//     const increment1 = computed(()=>{
//         return console.log(c1Arr.value);
//     })
//     const increment2 = computed(()=>{
//         return console.log(c1Arr.value);
//     })

//     return {
//         c1Arr,
//         getC1,
//         increment1,
//         increment2
//     }
// })

export default useCategoryStore
